package com.srpingde.hhh.http;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;

public abstract class HHHAsyncHttpResponseHandler extends AsyncHttpResponseHandler {
	
	private final static String TAG = "DaNingAsyncHttpResponseHandler";
	
	private String url;
	private Context context;
	private boolean willCache;
	
	public HHHAsyncHttpResponseHandler(Context context,String url,boolean cache){
		this.url = url;
		this.context = context;
		willCache = cache;
	}
	
	@Override
	public void onSuccess(String content) {
		Log.d(TAG, "######请求数据正确返回");
		if(content != null && content.equals("OK")){
			Log.d(TAG, "请求数据错误，获取缓存数据");
			//CacheJsonDao cache = new CacheJsonDao(context);
			//String cacheContent = cache.getCacheData(url);
			/*if(cacheContent == null){
				Log.d(TAG, "没有获取缓存数据");
				parseOnFailure();
			}else{
				Log.d(TAG, "返回缓存数据");
				//parseContent(cacheContent);
			}*/
			return;
		}
		if(willCache){
			/**
			 * zhao  留着 处理 缓存用
			 */
			//CacheJsonDao cache = new CacheJsonDao(context);
			//cache.insertCacheData(url, content);			
		}

		parseContent(content);
	}
	
	@Override
	public void onFailure(Throwable error,String content) {
		if(error != null && error.getCause()!= null){
			Log.d(TAG, error.getCause().toString());
		}
		
		if(error != null && error.getCause()!= null && error.getCause().toString().contains("ConnectTimeoutException")){
			//this.showDialog("网络连接超时，请检查网络。");
		}else{
			//this.showDialog("无法读取到相关数据，请检查网络后重试。");
		}
		
		Log.d(TAG, "请求数据错误，获取缓存数据");
		//CacheJsonDao cache = new CacheJsonDao(context);
		//String cacheContent = cache.getCacheData(url);
		/*if(cacheContent == null){
			Log.d(TAG, "没有获取缓存数据");
			parseOnFailure();
		}else{
			Log.d(TAG, "返回缓存数据");
			parseContent(cacheContent);
		}*/
	}
	
	/*public void onParseContentFailure(Exception e) {
		this.showDialog("数据错误");
	}*/
	
	/*private void showDialog(String msg){
		//AlertDialog alertDialog = DaNingApp.getInstance().getAlertDialog();
		if( alertDialog != null && alertDialog.isShowing() ){
			return;
		}else{
			AlertDialog.Builder build = new AlertDialog.Builder(context);
			build.setTitle("温馨提示");
			build.setMessage(msg);
			build.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
//					ad.dismiss();
				}
			});
			AlertDialog ad = build.create();
			//DaNingApp.getInstance().setAlertDialog(ad);
			ad.show();
		}
		
	}*/
	
	protected abstract void parseContent(String content);
	protected void parseOnFailure(){};
}
